package com.athuahua.spzx.controller;

import com.athuahua.spzx.dto.product.SkuSaleDto;
import com.athuahua.spzx.entity.ProductSku;
import com.athuahua.spzx.vo.ProductSkuVo;
import com.athuahua.spzx.dto.h5.ProductSkuDto;
import com.athuahua.spzx.service.ProductService;
import com.athuahua.spzx.utils.Result;
import com.athuahua.spzx.vo.h5.ProductItemVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: huahua
 * @Date: 2024/8/12 0012
 * @Time: 16:43
 * @Description:
 */
@Tag(name = "商品接口管理")
@RestController
@RequestMapping("/api/product")
@Slf4j
public class ProductController {

    @Resource
    private ProductService productService;



    @GetMapping("{page}/{limit}")
    @Cacheable(cacheNames = "productPageInfo::Phone",
            key = "{#p0, #p1 , #p2.category1Id , #p2.order}",
            condition = "#p2.brandId == null  && #p2.category2Id == null && #p2.category3Id == null &&  #p2.keyword.trim().length() == 0"
    )
    public Result getProductList(@PathVariable("page") Integer page,
                                 @PathVariable("limit") Integer limit,
                                 ProductSkuDto productSkuDto
    ) {
        log.info("getProductList page:{}, limit:{} productSkuDto:{}", page, limit , productSkuDto);
        IPage<ProductSkuVo> Page = productService.PageInfo(page,limit,productSkuDto);

        Map<String, Object> map = new HashMap<>();
        map.put("list", Page.getRecords());
        map.put("total", Page.getTotal());


        return Result.success(map);

    }


    @GetMapping("item/{skuId}")
    public Result item(@PathVariable("skuId") Long skuId){
        log.info("item skuId:{}", skuId);
        ProductItemVo productItemVo = productService.item(skuId);



        return Result.success(productItemVo);
    }

    /**
     * 用于远程调用，根据skuId返回sku信息
     */
    @GetMapping("getBySkuId/{skuId}")
    public ProductSku getBySkuId(@PathVariable("skuId") Long skuId){
        log.info("getBySkuId skuId:{}", skuId);
        ProductSku productSku = productService.getBySkuId(skuId);
        return productSku;
    }



//    @GetMapping("auth/test")
//    public Result test(){
//        log.info("test");
//        return Result.success("test");
//    }


    /**
     * openfeign调用 更新sku商品数量
     * @param skuSaleDtoList
     * @return
     */
    @Operation(summary = "更新商品sku销量")
    @PostMapping("updateSkuSaleNum")
    public Boolean updateSkuSaleNum(@RequestBody List<SkuSaleDto> skuSaleDtoList) {
        return productService.updateSkuSaleNum(skuSaleDtoList);
    }



}
